#property indicator_chart_window
#property indicator_buffers 6
#property indicator_color1 Chartreuse
#property indicator_color2 Orange
#property indicator_color3 Chartreuse
#property indicator_color4 Orange
#property indicator_color5 Chartreuse
#property indicator_color6 Orange

extern int Length = 20;
extern int Deviation = 2;
extern double MoneyRisk = 1.0;
extern int Signal = 1;
extern int Line = 1;
extern int Nbars = 1000;
extern int ArrowWidth = 1;
double gda_104[];
double gda_108[];
double gda_112[];
double gda_116[];
double gda_120[];
double gda_124[];

int init() {
   SetIndexBuffer(0, gda_104);
   SetIndexBuffer(1, gda_108);
   SetIndexBuffer(2, gda_112);
   SetIndexBuffer(3, gda_116);
   SetIndexBuffer(4, gda_120);
   SetIndexBuffer(5, gda_124);
   SetIndexStyle(0, DRAW_ARROW);
   SetIndexStyle(1, DRAW_ARROW);
   SetIndexStyle(2, DRAW_ARROW,EMPTY, ArrowWidth);
   SetIndexStyle(3, DRAW_ARROW,EMPTY, ArrowWidth);
   SetIndexStyle(4, DRAW_LINE);
   SetIndexStyle(5, DRAW_LINE);
   SetIndexArrow(0, 159);
   SetIndexArrow(1, 159);
   SetIndexArrow(2, 233);
   SetIndexArrow(3, 234);
   IndicatorDigits(MarketInfo(Symbol(), MODE_DIGITS));
   string ls_0 = "BBands Stop(" + Length + "," + Deviation + ")";
   IndicatorShortName(ls_0);
   SetIndexLabel(0, "UpTrend Stop");
   SetIndexLabel(1, "DownTrend Stop");
   SetIndexLabel(2, "UpTrend Signal");
   SetIndexLabel(3, "DownTrend Signal");
   SetIndexLabel(4, "UpTrend Line");
   SetIndexLabel(5, "DownTrend Line");
   SetIndexDrawBegin(0, Length);
   SetIndexDrawBegin(1, Length);
   SetIndexDrawBegin(2, Length);
   SetIndexDrawBegin(3, Length);
   SetIndexDrawBegin(4, Length);
   SetIndexDrawBegin(5, Length);
   return (0);
}

int start() {
   int li_8;
   double lda_12[25000];
   double lda_16[25000];
   double lda_20[25000];
   double lda_24[25000];
   for (int li_4 = Nbars; li_4 >= 0; li_4--) {
      gda_104[li_4] = 0;
      gda_108[li_4] = 0;
      gda_112[li_4] = 0;
      gda_116[li_4] = 0;
      gda_120[li_4] = EMPTY_VALUE;
      gda_124[li_4] = EMPTY_VALUE;
   }
   for (li_4 = Nbars - Length - 1; li_4 >= 0; li_4--) {
      lda_12[li_4] = iBands(NULL, 0, Length, Deviation, 0, PRICE_CLOSE, MODE_UPPER, li_4);
      lda_16[li_4] = iBands(NULL, 0, Length, Deviation, 0, PRICE_CLOSE, MODE_LOWER, li_4);
      if (Close[li_4] > lda_12[li_4 + 1]) li_8 = 1;
      if (Close[li_4] < lda_16[li_4 + 1]) li_8 = -1;
      if (li_8 > 0 && lda_16[li_4] < lda_16[li_4 + 1]) lda_16[li_4] = lda_16[li_4 + 1];
      if (li_8 < 0 && lda_12[li_4] > lda_12[li_4 + 1]) lda_12[li_4] = lda_12[li_4 + 1];
      lda_20[li_4] = lda_12[li_4] + (MoneyRisk - 1.0) / 2.0 * (lda_12[li_4] - lda_16[li_4]);
      lda_24[li_4] = lda_16[li_4] - (MoneyRisk - 1.0) / 2.0 * (lda_12[li_4] - lda_16[li_4]);
      if (li_8 > 0 && lda_24[li_4] < lda_24[li_4 + 1]) lda_24[li_4] = lda_24[li_4 + 1];
      if (li_8 < 0 && lda_20[li_4] > lda_20[li_4 + 1]) lda_20[li_4] = lda_20[li_4 + 1];
      if (li_8 > 0) {
         if (Signal > 0 && gda_104[li_4 + 1] == -1.0) {
            gda_112[li_4] = lda_24[li_4];
            gda_104[li_4] = lda_24[li_4];
            if (Line > 0) gda_120[li_4] = lda_24[li_4];
         } else {
            gda_104[li_4] = lda_24[li_4];
            if (Line > 0) gda_120[li_4] = lda_24[li_4];
            gda_112[li_4] = -1;
         }
         if (Signal == 2) gda_104[li_4] = 0;
         gda_116[li_4] = -1;
         gda_108[li_4] = -1.0;
         gda_124[li_4] = EMPTY_VALUE;
      }
      if (li_8 < 0) {
         if (Signal > 0 && gda_108[li_4 + 1] == -1.0) {
            gda_116[li_4] = lda_20[li_4];
            gda_108[li_4] = lda_20[li_4];
            if (Line > 0) gda_124[li_4] = lda_20[li_4];
         } else {
            gda_108[li_4] = lda_20[li_4];
            if (Line > 0) gda_124[li_4] = lda_20[li_4];
            gda_116[li_4] = -1;
         }
         if (Signal == 2) gda_108[li_4] = 0;
         gda_112[li_4] = -1;
         gda_104[li_4] = -1.0;
         gda_120[li_4] = EMPTY_VALUE;
      }
   }
   return (0);
}